package com.ls.springbootdemo.config;

import com.ls.springbootdemo.common.exception.MyException;
import com.ls.springbootdemo.common.response.MyResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author 挥之以墨
 */
@ControllerAdvice
@RestControllerAdvice
@Slf4j
public class MyExceptionHandler {

    @ExceptionHandler(MyException.class)
    public MyResponse handleMyException(MyException e) {
        log.error("自定义异常：", e);
        return MyResponse.buildFailure(e.getErrMsg());
    }

    @ExceptionHandler(RuntimeException.class)
    public MyResponse handleRuntimeException(RuntimeException e) {
        log.error("运行时异常：", e);
        return MyResponse.buildFailure(e.getMessage());
    }

}
